package cn.gailvlun.gll.presentation.emotion.notification

import android.view.View
import android.widget.LinearLayout
import cn.gailvlun.gll.chatlib.bean.SystemNotification
import cn.gailvlun.gll.chatlib.db.ChatDB2
import cn.gailvlun.gll.net.HttpMethods
import cn.gailvlun.gll.net.HttpSubscriber
import cn.gailvlun.gll.net.emotion.Emotion
import cn.gailvlun.gll.presentation.base.BaseActivity
import cn.gailvlun.gll.presentation.emotion.core.BaseAdapter
import cn.gailvlun.gll.presentation.emotion.core.BaseViewModel
import cn.gailvlun.gll.util.DialogUtil
import cn.gailvlun.gll.util.RxUtil
import cn.gailvlun.gll.util.ToastUtil
import io.reactivex.Flowable
import io.reactivex.android.schedulers.AndroidSchedulers
import io.realm.Realm
import io.realm.RealmResults
import io.realm.Sort
import java.util.concurrent.TimeUnit


class EmotionNotificationViewModel(
        activity: BaseActivity, adapter: BaseAdapter<Any>?
) : BaseViewModel(activity, adapter) {
    override var mActivity: BaseActivity = activity
    var realm: Realm?

    init {
        realm = Realm.getInstance(ChatDB2.getConfiguration())
    }


    fun jump2Profile(notification: SystemNotification) {
        val profile = realm?.copyFromRealm(notification.from)
        if (profile != null) {
            super.baseJump2Profile(profile)
        }
    }

    fun toEmotionDetail(notification: SystemNotification) {
        
        HttpMethods.getEmotionService()
                .emotionDetail(notification.emotion.id)
                .compose(RxUtil.applyScheduler())
                .subscribe(object : HttpSubscriber<Emotion>() {
                    override fun onSuccess(response: Emotion) {
                        baseToEmotionDetail(notification.emotion)
                    }

                    override fun onFailure(errMsg: String?, response: Emotion?, code: Int) {
                        if (code == 404) {
                            ToastUtil.showShort("该内容已经被删除")
                        } else {
                            DialogUtil.showError(mActivity, errMsg)
                        }
                    }
                })

    }

    fun loadMore(view: View) {
        (view as LinearLayout).visibility = View.GONE
        (mActivity as EmotionNotificationActivity).loadMore()
    }

    fun searchSystemNotification(): Flowable<RealmResults<SystemNotification>>? {
        return realm?.where(SystemNotification::class.java)
                ?.sort("created", Sort.DESCENDING)
                ?.findAllAsync()
                ?.asFlowable()
                ?.filter { it.isLoaded }
                ?.sample(1, TimeUnit.SECONDS)
                ?.observeOn(AndroidSchedulers.mainThread())
    }


    fun readEmotionNotification() {
        val results = realm!!.where(SystemNotification::class.java)
                .equalTo("unread", true)
                .and()
                .beginGroup()
                .equalTo("type", "emotionHeart")
                .or()
                .equalTo("type","emotionComment")
                .endGroup()
                .isNotNull("emotion")
                .findAll()
        realm!!.executeTransaction {
            for (emotionNoti in results) {
                emotionNoti.isUnread = false
            }
        }
    }

    override fun onCleared() {
        super.onCleared()
        readEmotionNotification()
        realm?.close()
        realm = null
    }


}